<template>
  <el-dialog v-model="visible" title="批量导入" width="600" show-footer show-zoom resize @close="close">
    <div class="row">
      <div class="row-title">第 1 步：点击下载模版</div>
      <a :href="url" class="row-main text-center pt-3 pb-3 block bg-[#FAFAFA]">
        <span class="text-blue-500"><svg-icon class="mr-1 inline-block" icon-class="download01"></svg-icon> 下载模版</span>
      </a>
    </div>
    <div class="row">
      <div class="row-title">第 2 步：按照模版要求填写</div>
      <div class="row-main"></div>
    </div>
    <div class="row">
      <div class="row-title">第 3 步：上传填写完成的文件 <template v-if="messageInfo">({{ messageInfo.message }} <a
            :href="messageInfo.data" class="text-blue-500">下载导入结果</a>)</template></div>
      <div class="flex justify-between uploadBox">
        <upload :api="props.api" :limit="limit" :validator="validator" type="channel" :id="export_id" @success="success">
          <el-button type="primary" link class="flex items-center">
            <el-icon>
              <Plus />
            </el-icon> 上传文件
          </el-button>
        </upload>
      </div>
    </div>

    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save()">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup name="importFile">
import { computed, reactive, ref, toRefs } from 'vue'
import upload from './upload.vue'
const visible = ref(false)
const loading = ref(false)
const props = defineProps({
  api: { type: Function, default: () => ([]) },
  type: { type: String, default: '' },
})
const data = reactive({
  limit: 1, // 最大文件上传数
  form: {},
  messageInfo: null,
  export_id: ''
})
const { limit, form, messageInfo,export_id } = toRefs(data)


const typeMap = new Map([
  ['warehouse', 'https://xc-goods.oss-cn-shenzhen.aliyuncs.com/wms/template/warehouse.xlsx'],  // 仓库
  ['location', 'https://xc-goods.oss-cn-shenzhen.aliyuncs.com/wms/template/location.xlsx'],  // 货位
  ['ware-sku', 'https://xc-goods.oss-cn-shenzhen.aliyuncs.com/wms/template/ware_sku_map.xlsx'],  // 第三方映射
  ['irobot-box-sku', 'https://xc-goods.oss-cn-shenzhen.aliyuncs.com/wms/template/irobotbox_map.xlsx'],  // 赛盒映射
  ['delivery-list', 'https://xc-goods.oss-cn-shenzhen.aliyuncs.com/wms/template/GoodsOrderBoxDataImport.xls'],  // 头程配货单
  ['swing-box', 'https://xc2k-wms.oss-cn-shenzhen.aliyuncs.com/tpl/ContainerManagesImport.xlsx'],  // 周转箱
])

const url = computed(() => {
  return typeMap.get(props.type)
})

const open = (id) => {
  export_id.value = id
  visible.value = true
}

const success = (obj) => {
  messageInfo.value = {
    message: obj.message,
    data: obj.data
  }
}
// 上传校验
const validator = (fileInfo) => {
  return (() => {

  })()
}

const emits = defineEmits(['query'])

// 文件上传
const save = async () => {
  emits('query')
  close()
}
// const handleRemove = (index) => {
//   fileList.value.splice(index, 1)
// }

const close = () => {
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.uploadBox {
  height: 52px;
  border-radius: 4px;
  border: 1px dashed #3366F1;
  justify-content: center;
  align-items: center;

  .v-upload {
    width: 100%;
    height: 100%;

    :deep(.el-upload) {
      width: 100%;
      height: 52px;
    }
  }
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.upload-file-uploader {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100%;
  height: 178px;
  text-align: center;
  border: 1px dashed var(--el-border-color);
  display: inline-block;
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-upload-list) {
  width: 100%;
  display: flex;
  flex-direction: column;

  .el-upload-list__item {
    width: 100%;
    padding: 3px 50px;
  }
}

.row {
  .row-title {
    font-size: 14px;
    font-weight: bold;
    color: #0A1431;
    line-height: 20px;
    padding: 8px 0;
  }
}

.upload-area {
  background: #FAFAFA;
  border-radius: 4px;

  .upload-tips {
    font-size: 12px;
    font-weight: 400;
    color: #9DA1AD;
    line-height: 17px;
    text-align: center;
    padding: 4px 0;
  }
}
</style>